﻿using Glue;
using Xunit;

namespace GlueTests.MappingTests
{
    public class When_adding_a_relation_between_arrays_of_nested_types
    {
        private readonly Mapping<DomainPerson, GuiPerson> mapping;

        public class DomainPerson { public DomainAddress[] Addresses { get; set; } }
        public class DomainAddress { }

        public class GuiPerson { public GuiAddress[] Addresses { get; set; } }
        public class GuiAddress { }

        public When_adding_a_relation_between_arrays_of_nested_types()
        {
            mapping = new Mapping<DomainPerson, GuiPerson>();
        }

        [Fact]
        public void Should_not_relate_without_converter_for_nested_type()
        {
            Assert.Throws(typeof(GlueException), ()=> mapping.Relate(domain => domain.Addresses, gui => gui.Addresses));
        }

        [Fact]
        public void Should_relate_when_converter_for_nested_type_is_added_when_relating()
        {
            mapping.Relate(domain => domain.Addresses, gui => gui.Addresses, GetMappingWithCreators());
        }

        [Fact]
        public void Should_relate_when_converter_for_nested_type_is_added_to_container()
        {
            mapping.AddConverter(GetMappingWithCreators());
            mapping.Relate(domain => domain.Addresses, gui => gui.Addresses);   
            
        }

        [Fact]
        public void Should_throw_exception_when_converter_is_added_to_container_but_converter_does_not_have_creators()
        {
            mapping.AddConverter(new Mapping<DomainAddress, GuiAddress>());
            Assert.Throws<GlueException>(() =>
                mapping.Relate(domain => domain.Addresses, gui => gui.Addresses)
            );
        }

        private Mapping<DomainAddress, GuiAddress> GetMappingWithCreators()
        {
            return new Mapping<DomainAddress, GuiAddress>(x => new DomainAddress(), y => new GuiAddress());
        }
    }
}
